﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>

    <link data-require="bootstrap-css@3.2.0" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
    <script data-require="angular.js@1.2.25" data-semver="1.2.25" src="https://code.angularjs.org/1.2.25/angular.js"></script>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
    <script src="jquery-2.1.4.min.js"></script>

    <script src="pageSelect.directive.js"></script>
    <script src=smart-table.debug.js></script>

</head>
<body ng-app="myApp1" ng-controller="checkCtrl">
    <div>
        <section>
            <pre>{{choseArr}}</pre>
            全选: <input type="checkbox" ng-model="master" ng-click="all(master,tesarry)">
            <div ng-repeat="z in tesarry">
                <input id={{z}} type="checkbox" ng-model="x" ng-checked="master" ng-click="chk(z,x)">{{z}}
            </div>
            <a href="#" class="btn btn-danger" ng-click="delete()"> 删除</a>
        </section>
    </div>
    <script>
        angular.module('myApp1', [])
            .controller('checkCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
                $scope.tesarry = ['1', '2', '3', '4', '5'];//初始化数据
                $scope.choseArr = [];//定义数组用于存放前端显示
                var str = "";//
                var flag = '';//是否点击了全选，是为a
                $scope.x = false;//默认未选中
                $scope.master = false;

                $scope.all = function (isAll, allId) {//全选
                    $scope.master = !isAll;
                    if (isAll != true) {
                        $scope.x = !isAll;
                        $scope.choseArr = allId.concat();
                    } else {
                        $scope.x = !isAll;
                        $scope.choseArr = [];
                    }

                    flag = 'a';
                };

                $scope.chk = function (z, x) {//单选或者多选

                    var arr = $scope.choseArr;
                    //if (flag == 'a') {//在全选的基础上操作
                    //    str = $scope.choseArr.join(',') + ',';
                    //}
                    if (x != true) {//选中
                        //str = str + z + ',';
                        arr.push(z);

                    } else {
                        str = str.replace(z + ',', ',');//取消选中
                        var index = -1;
                        for (var i = 0; i < arr.length; i++) {
                            if (z == arr[i]) {
                                index = i;
                                break;
                            }
                        }
                        if (index != -1)
                            arr.splice(index, 1);
                    }

                    //$scope.choseArr = (str.substr(0, str.length - 1)).split(',');

                };
                $scope.delete = function () {// 操作CURD

                    if ($scope.choseArr[0] == "" || $scope.choseArr.length == 0) {//没有选择一个的时候提示
                        alert("请至少选中一条数据在操作！")
                        return;
                    };

                    for (var i = 0; i < $scope.choseArr.length; i++) {
                        //alert($scope.choseArr[i]);
                        console.log($scope.choseArr[i]);//遍历选中的id
                    }
                };
            }])
    </script>
</body>
</html>
